package com.demo.client;

import java.util.Properties;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

/**
 * the class is manager the notification service and to load the configuration 
 * 
 * @author Administrator
 *
 */
public final class ServiceManager {

	private static final String TAG = LogUtil.getClassName(ServiceManager.class);
	
	private  SharedPreferences sharePre = null;
	
	private Context context = null;
	
	public ServiceManager(Context context)
	{
		this.context = context;
		
		String apiKey = null ;
		String xmppHost = null ;
		String xmppPort = null ;
		
		Properties prop = null;
		prop = loadPropperties();
		if(prop!=null)
		{
			apiKey = prop.getProperty("apiKey","");
			xmppHost = prop.getProperty("xmppHost","127.0.0.1");
			xmppPort = prop.getProperty("xmppPort","5222");
		}
		Log.i(TAG, "apikey:"+apiKey);
		Log.i(TAG, "xmppHost:"+xmppHost);
		Log.i(TAG, "xmppPort"+xmppPort);
		
		sharePre = context.getSharedPreferences(Constants.SHARE_PREFERENCE_NAME, Context.MODE_PRIVATE);
		Editor editor = sharePre.edit();
		editor.putString(Constants.API_KEY, apiKey);
		editor.putString(Constants.XMPP_HOST, xmppHost);
		editor.putString(Constants.XMPP_PORT, xmppPort);
		editor.commit();
	}
	
	/**
	 * 加载property 文件的内容
	 * 
	 * @return
	 */
	private Properties loadPropperties()
	{
		int id = context.getResources().getIdentifier("androidpn", "raw", context.getPackageName());
		
		Properties prop = new Properties();
		try {
			prop.load(context.getResources().openRawResource(id));
		} catch (Exception e) {
			e.printStackTrace();
			Log.d(TAG, "Can not find file~~");
		}
		return prop;
	}
	
	/**
	 * 设置提醒的标志icon 
	 * 
	 * @param iconID 标志res~id
	 */
	public void setNotificationIconId(int iconID)
	{
		Editor editor = sharePre.edit();
		editor.putInt(Constants.NOTIFICATION_ICON, iconID);
		editor.commit();
	}
	/**
	 * 开启服务
	 */
	public void startService()
	{
		Thread threadService = new Thread(new Runnable() {
			@Override
			public void run() {
				Intent service = NotificationService.getIntent();
				 context.startService(service);
			}
		});
		threadService.start();
	}
	/**
	 * 关闭服务
	 */
	public void stopService()
	{
		Intent service = NotificationService.getIntent();
		context.stopService(service);
	}
	/**
	 * 
	 * 跳转到notification 设置界面
	 * 
	 * @param con
	 */
	public static void viewNotificationSettings(Context con)
	{
		Intent intent = new Intent().setClass(con, NotificationSettingsActivity.class);
		con.startActivity(intent);
	}
}
